package schema

import (
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
)

// UserDepartment 用户部门关联
type UserDepartment struct {
	ent.Schema
}

// Fields 用户部门关联字段
func (UserDepartment) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("id").
			Positive().
			Unique().
			Immutable(),
		field.Int64("user_id").
			StorageKey("user_id").
			Comment("用户ID"),
		field.Int64("department_id").
			StorageKey("department_id").
			Comment("部门ID"),
		field.Bool("is_leader").
			Default(false).
			Comment("是否部门负责人"),
		field.Time("created_at").
			Default(time.Now).
			Immutable(),
	}
}

// Edges 用户部门关联边
func (UserDepartment) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("user", User.Type).
			Ref("user_departments").
			Field("user_id").
			Required().
			Unique().
			Comment("关联用户"),
		edge.From("department", Department.Type).
			Ref("user_departments").
			Field("department_id").
			Required().
			Unique().
			Comment("关联部门"),
	}
}

// Indexes 索引
func (UserDepartment) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("user_id", "department_id").
			Unique(),
		index.Fields("department_id"),
	}
}

// Annotations of the ConversationMember.
func (UserDepartment) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.Annotation{
			Table: "user_departments",
		},
	}
}
